package cn.share.life.module.images.utils;

import cn.hutool.core.io.FileUtil;

import javax.net.ssl.*;
import java.io.File;
import java.security.cert.X509Certificate;
import java.util.*;

public class ImageFileUtils {

    public static Map<String, Boolean> IMAGE_EXTENSION;
    static {
        IMAGE_EXTENSION = new HashMap<>();
        IMAGE_EXTENSION.put("jpg", true);
        IMAGE_EXTENSION.put("jpeg", true);
        IMAGE_EXTENSION.put("png", true);
        IMAGE_EXTENSION.put("gif", true);
        IMAGE_EXTENSION.put("bmp", true);
        IMAGE_EXTENSION.put("webp", true);
    }

    public static String getWebPageUrl(String baseUrl, HtmlFidAndPathEnums enums){
        return baseUrl + "thread1022.php?fid=" + enums.getFid() + "&page=";
    }

    public static Map<String, String> getImageFileNameMap(String path) {
        Map<String, String> fileNameMap = new HashMap<>();
        File directory = new File(path);

        if (directory.exists() && directory.isDirectory()) {
            for (File file : Objects.requireNonNull(directory.listFiles())) {
                if (file.isDirectory()) {
                    fileNameMap.putAll(getImageFileNameMap(file.getAbsolutePath()));
                } else {
                    String type = file.getName().substring(file.getName().lastIndexOf(".") + 1);
                    if (IMAGE_EXTENSION.getOrDefault(type, false)) {
                        fileNameMap.put(file.getName(), file.getPath());
                    }
                }
            }
        }
        return fileNameMap;
    }

    /**
     * 获取所有图片文件
     * @param path 目录
     * @return 图片文件列表
     */
    public static List<File> getImageFileList(String path) {
        List<File> fileList = new ArrayList<>();
        File directory = new File(path);
        if (directory.exists() && directory.isDirectory()) {
            for (File file : Objects.requireNonNull(directory.listFiles())) {
                if (file.isDirectory()) {
                    fileList.addAll(getImageFileList(file.getAbsolutePath()));
                } else {
                    String type = file.getName().substring(file.getName().lastIndexOf(".") + 1);
                    if (IMAGE_EXTENSION.getOrDefault(type, false)) {
                        fileList.add(file);
                    }
                }
            }
        }
        return fileList;
    }

    public static List<String> getAllFileLinks(String path) {
        List<String> resultList = new ArrayList<>();
        File directory = new File(path);

        if (directory.exists() && directory.isDirectory()) {
            for (File file : Objects.requireNonNull(directory.listFiles())) {
                if (file.isDirectory()) {
                    resultList.addAll(getAllFileLinks(file.getAbsolutePath()));
                } else if (file.getName().equals("url.txt")) {
                    resultList.addAll(FileUtil.readLines(file, "UTF-8"));
                }
            }
        }
        return resultList;
    }

    /**
     * 获取所有url.txt文件
     * @param path 目录
     * @return url.txt文件列表
     */
    public static List<File> getUrlFileList(String path) {
        List<File> resultList = new ArrayList<>();
        File directory = new File(path);

        if (directory.exists() && directory.isDirectory()) {
            for (File file : Objects.requireNonNull(directory.listFiles())) {
                if (file.isDirectory()) {
                    resultList.addAll(getUrlFileList(file.getAbsolutePath()));
                } else if (file.getName().equals("url.txt")) {
                    resultList.add(file);
                }
            }
        }
        return resultList;
    }


    public static List<File> getUrlFileList(String path, String text) {
        List<File> resultList = new ArrayList<>();
        File directory = new File(path);

        if (directory.exists() && directory.isDirectory()) {
            for (File file : Objects.requireNonNull(directory.listFiles())) {
                if (file.isDirectory()) {
                    resultList.addAll(getUrlFileList(file.getAbsolutePath(), text));
                } else if (file.getName().equals(text)) {
                    resultList.add(file);
                }
            }
        }
        return resultList;
    }

    public static void disableSSLVerification() {
        try {
            // 创建一个信任所有证书的 TrustManager
            TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        public X509Certificate[] getAcceptedIssuers() {
                            return null;
                        }
                        public void checkClientTrusted(X509Certificate[] certs, String authType) {}
                        public void checkServerTrusted(X509Certificate[] certs, String authType) {}
                    }
            };

            // 安装 TrustManager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            // 创建一个 HostnameVerifier，忽略主机名验证
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };

            // 安装 HostnameVerifier
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
